﻿using RainFlowandDamageTool.Models;
using RainFlowandDamageTool.RainFlowCountingMethod;
using System;
using System.Collections.Generic;
using System.Text;

namespace RainFlowandDamageTool.ComputingProcess
{
    public static class TotalProcess
    {
        /// <summary>
        /// 统计雨流计数结果，不做损伤计算，损伤和计数结果分离开来，为了共用计数结果
        /// </summary>
        /// <param name="lst"></param>
        /// <returns></returns>
        public static LinkedList<RainFlowResult>? GetLinkedRainflowfromList(this List<double> lst)
        {
            if (lst != null)
            {
                var datapara = lst.GetDataPara();//获得源数据的各种参数
                var r1=lst.Findpeaks().HysteresisFilteringV2(datapara).GetBinning(datapara).List2LinkedList().GetCycle(datapara, 0, out List<double> residue).List2LinkedList().CombineCycle();//获得第一次的雨流计数结果和residue
                var r2 = residue.DuplicateResidue().Findpeaks().List2LinkedList().GetCycle(datapara, 1, out List<double> useless).List2LinkedList().CombineCycle();//获得residue的雨流计数结果
                foreach (var item in r2)
                {
                    r1.AddLast(item);
                }
                return r1;
            }
            else { return null; }
        }
        /// <summary>
        /// 计算损伤
        /// </summary>
        /// <param name="lst"></param>
        /// <param name="k1"></param>
        /// <param name="k2"></param>
        /// <returns></returns>
        public static double GetAccumDamagefromLinkedList(this LinkedList<RainFlowResult>? lst, double k1, double k2)
        {
            if(lst != null)
            {
               return lst.GetDamage(k1, k2).GetAccumDamage();
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 获得累积损伤（repeated block）
        /// </summary>
        /// <param name="lst">数据源</param>
        /// <returns>累计损伤</returns>
        public static double GetAccumDamagefromList(this List<double> lst,double k1,double k2)
        {
            double normaldamage = 0;
            double residuedamage = 0;
            if (lst != null)
            {
                var datapara = lst.GetDataPara();//获得源数据的各种参数
                normaldamage = lst.Findpeaks().HysteresisFilteringV2(datapara).GetBinning(datapara).List2LinkedList()
                    .GetCycle(datapara,0, out List<double>residue).List2LinkedList().CombineCycle()
                    .GetDamage(k1,k2).GetAccumDamage();//获得第一次的雨流计数结果和residue
                residuedamage = residue.DuplicateResidue().Findpeaks().List2LinkedList()
                    .GetCycle(datapara, 1, out List<double> useless)
                    .List2LinkedList().CombineCycle().GetDamage(k1,k2).GetAccumDamage();//获得residue的雨流计数结果
            }
            return normaldamage+residuedamage;
        }
        /// <summary>
        /// 除了雨流计数外还添加了rangepair的计数
        /// </summary>
        /// <param name="lst"></param>
        /// <param name="k1"></param>
        /// <param name="k2"></param>
        /// <param name="results"></param>
        /// <returns></returns>
        public static List<RainFlowResult> GetRPfromList(this List<double> lst)
        {
            List <RainFlowResult> results= new List <RainFlowResult>();
            if (lst != null)
            {
                var datapara = lst.GetDataPara();//获得源数据的各种参数
                var rp1 = lst.Findpeaks().HysteresisFilteringV2(datapara).List2LinkedList().GetCycle(datapara, 0, out List<double> residue);
                var rp2 = residue.DuplicateResidue().Findpeaks().List2LinkedList().GetCycle(datapara, 1, out List<double> useless);
                rp1.AddRange(rp2);
                return rp1;
            }
            else
            {
                return results;
            }
            
        }
    }
}
